package com.example.win.android_banduyujia.login.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.entity.LoginInfEntity;
import com.example.win.android_banduyujia.http.request.AsycHttpRequest;
import com.example.win.android_banduyujia.http.request.SigninRequest;
import com.example.win.android_banduyujia.http.response.SigninResponse;
import com.example.win.android_banduyujia.lib.util.StrUtils;
import com.example.win.android_banduyujia.login.application.ILoginStatusChangedListener;
import com.example.win.android_banduyujia.login.application.LoginApplication;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.OpenActivity;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseActivity;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.IBaseActivity;

/**
 * Created by Win on 2015/8/6.
 */
public class LoginActivity extends Activity implements View.OnClickListener{

    private EditText  EditPhone;
    private EditText  EditPassword;
    private Button    BtnLogin, BtnRegistered;
    private TextView  TxvForget;

    private String    mUserName;
    private String    mUserPassword;
    private String    mDevices;

    private AsycHttpRequest   mAsyncHttpClient = null;
    private BaseActivity      mContext;
    private LoginApplication  mLoginAppLication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mAsyncHttpClient = new AsycHttpRequest();
        mContext = new BaseActivity();
        mLoginAppLication = LoginApplication.getInstance();

        /*DisplayMetrics  dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int  density = (int) dm.density;
        int  nowWidth = dm.widthPixels;//当前分辨率宽度
        int  nowHeight = dm.heightPixels; // 当前分辨率高度
        int  width = (int)(nowWidth * density);
        int  height = (int)(nowHeight * density);*/

        InitViews();
    }

    private void InitViews() {
        EditPhone = (EditText) findViewById(R.id.activity_login_username);
        EditPassword = (EditText) findViewById(R.id.activity_login_password);
        BtnLogin = (Button) findViewById(R.id.activity_button_login);
        BtnRegistered = (Button) findViewById(R.id.activity_button_registered);
        TxvForget = (TextView) findViewById(R.id.activity_forget_password);
        setUnderLineOfText();

        BtnLogin.setOnClickListener(this);

        BtnRegistered.setOnClickListener(this);
        TxvForget.setOnClickListener(this);

        EditPhone.requestFocus();
        InputMethodManager imm= (InputMethodManager) EditPhone.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }

    private void setUnderLineOfText() {
        String string = getString(R.string.activity_layout_forget_password);
        SpannableString  spanString = new SpannableString(string);
        spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        TxvForget.setText(spanString);
    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.activity_button_login:  // 登录
                login();
               break;
           case R.id.activity_button_registered: // 注册
                registered();
               break;
           case R.id.activity_forget_password: // 忘记密码
                forgetPassword();
               break;
           }
    }

    private void login() {
         if(checkUserEnter()){
              SigninRequest request = new SigninRequest(mUserName, mUserPassword, StrUtils.createUUID());
              mAsyncHttpClient.doRequest((BaseActivity)mContext, request, SigninResponse.class, mLoginHandler, true);
         }
       // startActivity(new Intent(this, OpenActivity.class));
    }

    public Handler mLoginHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            LoginInfEntity mLoginInfo = (LoginInfEntity) msg.obj;
            Log.d("mLoginInfo", mLoginInfo.toString());
            mLoginAppLication.login(mLoginInfo);
            mContext.showToast("登录成功!");
            setResult(Activity.RESULT_OK);
            finish();
        }
    };

    private boolean checkUserEnter() {
        mUserName = EditPhone.getText().toString().trim();
        mUserPassword = EditPassword.getText().toString().trim();
        if(StrUtils.isEmpty(mUserName)){
            Toast.makeText(getApplicationContext(), R.string.activity_layout_edit_view_phone, Toast.LENGTH_SHORT).show();
            return false;
        }
        if(StrUtils.isEmpty(mUserPassword)){
            Toast.makeText(getApplicationContext(), R.string.login_activity_toast_enter_password, Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void registered() {
          startActivity(new Intent(this, RegisteredActivity.class));
    }

    private void forgetPassword() {
           startActivity(new Intent(this, ForgetActivity.class));
    }

    /*private long  mExitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序~~", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }*/
}
